#!/bin/bash

selfpath=$(cd $(dirname $0); pwd)

if [ `uname` = "Darwin" ]; then
    os_name="macos"
    rctarget=".bash_profile"
else
    os_name="linux"
    rctarget=".bashrc"
fi

function setrclink() {
    from=$selfpath/$2
    to=$HOME/$1

    to_dir=$(dirname $to)
    mkdir -p $to_dir

    if [ -L $to ]; then
        if [ $(readlink $to) = $from ]; then 
            echo "already done: $from -> $to"
        else
            echo "symbolic exists! $to"
        fi
    elif [ -e $to ]; then
        echo "target exists! $to "
    else
        echo "create link: $from -> $to"
        ln -s $from $to
    fi
}

setrclink .inputrc $os_name/inputrc.sh
setrclink .vimrc rc.vim
setrclink .vim vim
setrclink .gitconfig git/gitconfig
setrclink .config/nvim nvim
setrclink .config/fish/config.fish fish/config.fish
setrclink .aria2 aria2

echo -e "" >> $HOME/$rctarget
echo "export os_name=$os_name" >> $HOME/$rctarget
echo "source $selfpath/common_bashrc.sh" >> $HOME/$rctarget
echo "source $selfpath/$os_name/bashrc.sh" >> $HOME/$rctarget
echo "source $selfpath/aliases.sh" >> $HOME/$rctarget
echo "source $selfpath/git/git-completion.bash" >> $HOME/$rctarget

